﻿Imports System.ComponentModel
Imports System.IO

Public Class Form1

    Private _FileOpenPath As String = String.Empty
    Private _FileSavePath As String = String.Empty
    Private WithEvents oMyWorker As BackgroundWorker = New BackgroundWorker
    Private _RecordCount As Integer = 0


    Private Sub btnFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileOpen.Click
        Dim sFilePath As String = String.Empty
        Dim oDLGRes As DialogResult = Nothing
        Try
            oDLGRes = Me.OpenFileDialog1.ShowDialog(Me)
            If oDLGRes = DialogResult.OK Then
                sFilePath = Me.OpenFileDialog1.FileName
                If sFilePath.Contains("Auc-ScanData.lua") = False Then
                    MessageBox.Show("Sorry the selected file is not a valid auctioneer file please locate the file at World of Warcraft\WTF\Account\[Your account name]\SavedVariables\Auc-ScanData.lua")
                    Exit Sub
                Else
                    _FileOpenPath = sFilePath
                    txtAucLuaPath.Text = sFilePath
                End If
            End If
        Catch ex As Exception
            LogWriter.WriteLog(ex)
        End Try
    End Sub

    Private Sub btnFileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileSave.Click
        Dim sFilePath As String = String.Empty
        Dim oDLGRes As DialogResult = Nothing
        Try
            oDLGRes = Me.SaveFileDialog1.ShowDialog(Me)
            If oDLGRes = DialogResult.OK Then
                sFilePath = Me.SaveFileDialog1.FileName
                If sFilePath.Contains(".csv") = False Then
                    MessageBox.Show("Sorry the selected extension is not suported.")
                    Exit Sub
                Else
                    _FileSavePath = sFilePath
                    txtScanExportPath.Text = sFilePath
                End If
            End If
        Catch ex As Exception
            LogWriter.WriteLog(ex)
        End Try
    End Sub

    Private Sub btnDoWork_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoWork.Click
        Try
            'check both files have been selected
            If txtAucLuaPath.Text.Trim <> String.Empty And txtScanExportPath.Text.Trim <> String.Empty Then
                Me.oMyWorker.RunWorkerAsync()

                While Me.oMyWorker.IsBusy
                    System.Threading.Thread.Sleep(20)
                    ToolStripProgressBar1.Increment(1)
                    If ToolStripProgressBar1.Value = 100 Then
                        ToolStripProgressBar1.Value = 0
                    End If
                    ' Keep UI messages moving, so the form remains 
                    ' responsive during the asynchronous operation.
                    Application.DoEvents()
                End While
                MessageBox.Show("Done file read and data export file saved to " & Me._FileSavePath & " " & _RecordCount.ToString & " Records Exported.")
            Else
                MessageBox.Show("Please select a File paths for both import file and export file.")
            End If
        Catch ex As Exception
            LogWriter.WriteLog(ex)
            MessageBox.Show("A big Error has occured. Please try again.")
        End Try
    End Sub


    Private Sub oMyWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles oMyWorker.DoWork
        Dim oclsAucScanAdapter As New clsAucScanAdapter
        Dim oScanDataItemList As System.Collections.Generic.List(Of AuctionItem)
        Try
            oScanDataItemList = oclsAucScanAdapter.Read_AucScanFromFilePath(_FileOpenPath)
            If oScanDataItemList.Count > 0 Then
                _RecordCount = oScanDataItemList.Count
                AuctionItemsCSVAdapter.Do_Export(oScanDataItemList, _FileSavePath)
            Else
                MessageBox.Show("There are no records found please try again.")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub oMyWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles oMyWorker.RunWorkerCompleted
        ToolStripProgressBar1.Value = 0
    End Sub
End Class
